package org.rbreznak.tronco.client;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.rbreznak.tronco.sql.SQLData;

public class Screen4 extends Screen{
	private static final long serialVersionUID = 1L;

	private JLabel statusText = new JLabel();
	
	private String id;
	
	private JPanel panel = new JPanel();
	
	private ArrayList<projBtn> projectButtons = new ArrayList<projBtn>();
	
	private JButton btnExit = new JButton(new ImageIcon("img/buttons/exit.png"));
	private GridBagConstraints c = new GridBagConstraints();
	
	private Hashtable userData = new Hashtable();
	
	public Screen4() {
		setLayout(new BorderLayout());
		setBackground(new Color(0,0,0));
		
		panel.setBackground(new Color(0,0,0));
		panel.setLayout(new GridBagLayout());
		
		
		FormatUI.formatLabel(statusText); 
		
		add(statusText, BorderLayout.NORTH);
		add(panel, BorderLayout.CENTER);
		
		this.setVisible(true);
	}

	public void setID(String id) {
		this.id = id;
	}
	
	public void init() {
		SQLData dat;
		
		dbh.execute("SELECT * FROM users WHERE `userID` = '" + id + "' LIMIT 1;");
		
		dat = dbh.getResults();
		if(dat.getResults().size() == 0) {
			// EXIT!
			_fireScreenUpdate();
		}
		
		userData = (Hashtable) dat.next();
		
		btnExit.setAlignmentX(Component.RIGHT_ALIGNMENT);
		
		FormatUI.formatImageButton(btnExit);
		
		add(btnExit, BorderLayout.SOUTH);
		
		statusText.setText("<html><body><center>Welcome " + userData.get("userFirst") + 
				"<br>Please Pick A Project</center></body></html>");
		
		getProjects(1);
	}
	
	public void getProjects(int page) {
		if(page < 1) { page = 1; }
		SQLData results;
		Hashtable data;

		dbh.execute("SELECT * FROM projects WHERE `projectUser` = '" + id + "' LIMIT "+ (page-1)*9 + ", 9;");
		results = dbh.getResults();
		
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridy = -3;
		c.weightx = 0.5;
		c.ipady = 5; 
		
		int i=0;
		
		while(results.hasNext())
		{
			data = (Hashtable) results.next();
			projBtn button = new projBtn(new ImageIcon("img/buttons/medium_blank.png") );
			
			FormatUI.formatImageButton(button);
			
			button.setText((String) data.get("projectName"));
			button.id = (String) data.get("projectID");
			button.setHorizontalTextPosition(JButton.CENTER);
			button.setVerticalTextPosition(JButton.CENTER);

			c.gridx = (i)%3;

			panel.add(button,c);
			projectButtons.add(button);
			i++;
		}
		
		if(dbh.numRows() < 9) {
			projBtn button = new projBtn(new ImageIcon("img/buttons/medium_blank.png") );
			
			FormatUI.formatImageButton(button);
			
			button.setText("New Project");
			button.id = "0";
			button.setHorizontalTextPosition(JButton.CENTER);
			button.setVerticalTextPosition(JButton.CENTER);
			
			panel.add(button,c);
			projectButtons.add(button);
		}
		
		for(i=0;i<projectButtons.size();i++) {
			projectButtons.get(i).addActionListener(this);
		}
		
	}
	
	public void actionPerformed(ActionEvent arg0) {
		((Screen5) nodes.get(0)).setID(((projBtn) arg0.getSource()).id);
		_fireScreenUpdate();
	}

	@Override
	public void decideNext() {
		if(nodes.size() > 0) {
			setNext(nodes.get(0));
		}
	}
	
	@Override
	public String toString() {
		return id;
	}

	@Override
	public void reset() {
		panel.removeAll();
		projectButtons.clear();
		setNext(null);
	}
	
	private class projBtn extends JButton{
		private String id;
		
		public projBtn(ImageIcon icon) {
			super(icon);
		}
	}
}
